/**
 * 
 */
package gui.history;

import history.DataHistory;

import java.util.ListIterator;
import java.util.ResourceBundle;

import javax.swing.table.AbstractTableModel;

import common.Data;

/**
 * Table Model for JTable for displaying recent mesurements in tabular form.
 * 
 * @author mamut
 */
public class DataHistoryTableModel extends AbstractTableModel {

	private static final long serialVersionUID = -8570359526724326508L;
	private ResourceBundle msg;
	
	public DataHistoryTableModel(DataHistory historyProvider) {
		msg = ResourceBundle.getBundle("DataHistoryGUI");
		this.historyProvider = historyProvider;
	}

	private DataHistory historyProvider;
	
	/* Oh shut up! I know it looks like C but native Java enum would require to write switch returning enum depending on int value, so it sill wouldn't be nice - now I talk about switch in getValueAt(int, int) */
	private static final int DATE = 0;
	private static final int TEMPERATURE = 1;
	
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return historyProvider.getCount();
	}
	
//	/* (non-Javadoc)
//	 * @see javax.swing.table.TableModel#getColumnClass(int)
//	 */
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Override
//	public Class getColumnClass(int c) {
//		switch(c) {
//			case DATE:
//				return Date.class;
//			case TEMPERATURE:
//				return double.class;
//		}
//		return Object.class;
//	}
	
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int col) {
		switch(col) {
			case DATE: 
				return msg.getString("Date");
			case TEMPERATURE:
				return msg.getString("Temperature");
		}
		return null;
    }

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return 2;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int row, int col) {
		return false;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		ListIterator<Data> it = historyProvider.iteratorEnd();
		Data ob = it.previous();
		while(rowIndex>0 && it.hasPrevious()) {
			ob = it.previous();
			--rowIndex;
		}
		if(ob == null) 
			return null;
		if(rowIndex > 0)
			return null;
		switch(columnIndex) {
			case DATE:
				return ob.getDate();
			case TEMPERATURE:
				return ob.getTemperature();
		}
		return null;
	}

}
